import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Router} from "@angular/router";

@Injectable({
  providedIn: 'root'
})
export class PaygoodsService {
   public payGoodsmsg: any = [];
  constructor(private http: HttpClient, private router: Router) { }
  // 存储在购物车选中的商品
  setGoodsmsg( arr) {
    // 接收到选中商品id,根据id找到对应的商品信息
    this.payGoodsmsg = [];
    const url = 'http://192.168.2.228:8080/shoppingcart/personal?user_id=' + localStorage.user;
    this.http.get(url).subscribe( res => {
      console.log(res);
      let msgArr = res['data'];
      for ( let j = 0; j < arr.length; j ++) {
        for ( let i = 0; i < msgArr.length; i ++) {
          if ( arr[j] === msgArr[i].g_id) {
            this.payGoodsmsg.push( msgArr[i]);
          }
        }
      }
      sessionStorage.setItem("goodsMsg", JSON.stringify(this.payGoodsmsg));
      console.log("存储成功");
      this.router.navigate(['/paydetail']);
    });
  }
  getGoodsmsg() {
    return JSON.parse(sessionStorage.getItem("goodsMsg"));
  }
  delGoodsmsg() {
    sessionStorage.removeItem("goodsMsg");
  }
}
